package org.javaforever.cookieshop.controller;

import java.util.List;

import org.apache.poi.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.javaforever.cookieshop.domain.TypeBean;
import org.javaforever.cookieshop.domain.UserBean;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class UserCustController { 
	@Autowired
	protected GoodsService goodsService;
	@Autowired
	protected TypeService typeService;
	@Autowired
    private Environment env;
    
    @GetMapping("/go_user_register")
    public String goodsDetail(Model model) throws Exception{
	    List<TypeBean>typeList= typeService.listActiveTypes();
	    model.addAttribute("typeList",typeList);
        return "user_register";
    }
    
    @GetMapping("/user_login")
    public String userLogin(Model model) throws Exception{
	    List<TypeBean>typeList= typeService.listActiveTypes();
	    model.addAttribute("typeList",typeList);
        return "user_login";
    }
    
    @GetMapping("/user_logout")
    public String userLogout(Model model) throws Exception{
        UserBean user = (UserBean)SecurityUtils.getSubject().getPrincipal();
        SecurityUtils.getSubject().logout();
        String projectName = env.getProperty("spring.application.name");
        if (StringUtil.isBlank(projectName) || "null".equals(projectName)) projectName = "";
    	return "redirect:"+projectName+"/index";
    }
    
    @GetMapping("/user_center")
    public String userCenter(Model model) throws Exception{
	    List<TypeBean>typeList= typeService.listActiveTypes();
	    model.addAttribute("typeList",typeList);
        return "user_center";
    }
 }
